package org.jplearning.io;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;

public class PruebaSAXFilter extends XMLFilterImpl{
	/** The URI of the namespace to remove */
	  private String namespaceURI;

	  public PruebaSAXFilter(XMLReader reader, String namespaceURI) {
	    super(reader);
	    this.namespaceURI = namespaceURI;
	  }

	  public void startElement(String uri, String localName, String qName,
	                           Attributes atts)
	    throws SAXException {

	    // If this element is in the namespace to remove, skip it
	    if (namespaceURI.equals(uri)) {
	      // Do nothing, which skips this element
	    } else {
	      // Make a copy that we can modify
	      org.xml.sax.helpers.AttributesImpl newAtts =
	        new org.xml.sax.helpers.AttributesImpl();

	      // Remove (don't add to the new list) any attribute with the given name
	      for (int i=0; i<atts.getLength(); i++) {
	        String attURI = atts.getURI(i);
	        if (attURI.equals(namespaceURI)) {          // skip it
	        } else {
	          newAtts.addAttribute(attURI, atts.getLocalName(i),
	            atts.getQName(i), atts.getType(i), atts.getValue(i));
	        }
	      }
	       
	      // Delegate to the normal parsing behavior
	      super.startElement(uri, localName, qName, newAtts);
	    }
	  }

	  public void endElement(String uri, String localName, String qName) 
	    throws SAXException {

	    if (namespaceURI.equals(uri)) {
	      // Do nothing, which skips this element
	    } else {
	      // Delegate to normal parsing behavior
	    	super.endElement(uri, localName, qName);
	    }
	  }
}
